﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using MaterialDemo.IService.IPermission;
using MaterialDemo.Model.PowerDTO;
using MaterialDemo.Model.RoleDTO;
using MaterialDemo.MVC.Filters;
using MaterialDemo.Utility;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;

namespace MaterialDemo.MVC.Controllers
{
    [MySession]
    public class RoleController : Controller
    {
        #region  构造函数注入
        private readonly ILogger<RoleController> _logger;
        private readonly IRoleService _roleService;
        private readonly IPowerService _powerService;

        public RoleController(ILogger<RoleController> logger, IRoleService roleService,IPowerService powerService)
        {

            _logger = logger;
            _roleService = roleService;
            _powerService = powerService;

        }
        #endregion



        #region 页面

        /// <summary>
        /// 角色信息页面
        /// </summary>
        /// <returns></returns>
        public IActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 添加角色页面
        /// </summary>
        /// <returns></returns>
        public IActionResult AddIndex()
        {

            return View();
        }

        /// <summary>
        /// 修改角色页面
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IActionResult UpdateIndex(int id)
        {
            RoleOutput model = _roleService.LoadEntities(id).FirstOrDefault();
            ViewBag.model = model;
            return View();
        }



        /// <summary>
        /// 给用户配置角色的页面
        /// </summary>
        public IActionResult ConfigIndex(string userid)
        {
            ViewBag.userid = userid;
            ViewBag.role = _roleService.LoadEntities();
            return View();
        }

        /// <summary>
        /// 给角色分配权限页面
        /// </summary>
        /// <returns></returns>
        public IActionResult PowerIndex(string roleId)
        {
            ViewBag.roleId = roleId;
            return View();
        }
        #endregion
        #region 非页面

        /// <summary>
        /// 获取角色信息,不分页
        /// </summary>
        /// <returns></returns>
        public string GetJsonList1()
        {


            List<RoleOutput> outlist = _roleService.LoadEntities();
            DataResult<List<RoleOutput>> dataResult = new DataResult<List<RoleOutput>>()
            {
                code = 0,
                msg = "ok",
                data = outlist,
            };
            return JsonNetHelper.SerializetoJson(dataResult);
        }

        /// <summary>
        /// 获取角色分页信息
        /// </summary>
        /// <returns></returns>
        public string GetJsonList(int page, int limit, string selectInfo)
        {

            int totalCount = 0;

            List<RoleOutput> outlist = _roleService.LoadPageEntities(page, limit, out totalCount, selectInfo);
            DataResult<List<RoleOutput>> dataResult = new DataResult<List<RoleOutput>>()
            {
                code = 0,
                msg = "ok",
                count = totalCount,
                data = outlist,
            };
            return JsonNetHelper.SerializetoJson(dataResult);
        }
        /// <summary>
        /// 给用户配置角色的方法
        /// </summary>
        /// <param name="strjson"></param>
        /// <returns></returns>
        public string ConfigRole(string userID, string arrRoleID)
        {
            if (_roleService.ConfigRole(userID, arrRoleID))
            {
                return "操作成功";
            }
            else
            {
                return "操作失败";
            }

        }

        /// <summary>
        /// 角色信息添加的方法
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public string Add(RoleInput inputEntity)
        {
           
            if (_roleService.Add(inputEntity) > 0)
            {
                return "ok";
            }
            else
            {
                return "no";
            }
        }

        /// <summary>
        /// 角色信息更新的方法
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public string Update(RoleInput inputEntity)
        {

            if (_roleService.Update(inputEntity) > 0)
            {

                return "ok";

            }
            else
            {
                return "no";
            }

        }
        /// <summary>
        /// 开启/禁用角色的方法
        /// </summary>
        /// <param name="id"></param>
        /// <param name="flag"></param>
        /// <returns></returns>
        public string ForbidRole(int id, string flag)
        {
            if (_roleService.ForbidRole(id, flag))
            {
                if (flag == "true")
                {
                    return "角色已开启";
                }
                else
                {
                    return "角色已禁用";
                }

            }
            else
            {
                return "操作失败";
            }
        }

        /// <summary>
        /// 判断角色id是否重复
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public string CheckRepeat(string value)
        {

            if (_roleService.CheckRepeat(value))
            {
                //如果有记录，说明用户id重复，不可以
                return "no";
            }
            else
            {
                return "ok";
            }

        }

        /// <summary>
        /// 获取角色对应的权限
        /// </summary>
        /// <returns></returns>
        public string GetPowerList(string roleId)
        {
                
            PowerTreeOutput outlist = _powerService.GetPowerList(roleId);       
            var a= JsonNetHelper.SerialzeoJsonForCamelCase(outlist);
            return "["+a+"]";
        }

        /// <summary>
        /// 为角色配置权限
        /// </summary>
        /// <returns></returns>
        public string SetPower(string strId,string roleId)
        {

            if (_powerService.SetPower(strId,roleId))
            {
                return "ok";
            }
            else
            {
                return "no";
            }
            
        }
        #endregion
    }
}